package com.gelapps.memogel.credentials;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.app.Activity;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.gelapps.memogel.Constants;
import com.gelapps.memogel.Functions;
import com.gelapps.memogel.R;
import com.gelapps.memogel.password.FunctionsPass;

public class CredEdit extends Activity {

    private String sKey;
    private EditText edTxtPass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cred_edit);
        String credName="";
        Bundle bundle = this.getIntent().getExtras();
        if (bundle!=null)
        {
            this.sKey = bundle.getString(Constants.getKeyString());
            credName = bundle.getString(Constants.getCardsName());
        }
        else
            Functions.showLongToast(getApplicationContext(), getString(R.string.str_error_loading_inf));

        edTxtPass = (EditText) findViewById(R.id.editCredentialPassEdit);

        if (!credName.equals(""))
        {
            CredentialMaster cred = CredentialManager.getInstance().getCredential(credName);
            ((TextView) findViewById(R.id.txtCredNameViewEdit)).setText(cred.getName());
            ((EditText) findViewById(R.id.editCredentialUserEdit)).setText(FunctionsPass.decryptString(this.sKey, cred.getUser()));
            edTxtPass.setText(FunctionsPass.decryptString(this.sKey, cred.getPass()));
            ((EditText) findViewById(R.id.editCredentialDateCreatedEdit)).setText(cred.getDateCreated());
            ((EditText) findViewById(R.id.editCredentialDateLastUsedEdit)).setText(cred.getDateLastUsed());
            ((EditText) findViewById(R.id.editCredentialExpiryEdit)).setText(cred.getDataExpiry());
            ((EditText) findViewById(R.id.editCredentialSiteEdit)).setText(cred.getSite());
            ((EditText) findViewById(R.id.editCredentialNoteEdit)).setText(cred.getNote());
            ((Button) findViewById(R.id.btnCredSaveEdit)).setOnClickListener(editCredListener);
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            if (preferences.getBoolean(Constants.getPrefStringShowPass(), Constants.getPrefDefaultShowPass()))
            {
                ((ImageView) findViewById(R.id.imgViewCardCvvView)).setVisibility(View.GONE);
                ((EditText) findViewById(R.id.editCredentialUserEdit)).setInputType(InputType.TYPE_CLASS_TEXT );
                ((ImageView) findViewById(R.id.imgViewCardNumberView)).setVisibility(View.GONE);
                edTxtPass.setInputType(InputType.TYPE_CLASS_NUMBER);
            }
            ((ImageView) findViewById(R.id.imgViewCardCvvView)).setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view) {
                    if (((EditText) findViewById(R.id.editCredentialUserEdit)).getInputType()!= InputType.TYPE_CLASS_TEXT )
                    {
                        ((ImageView) findViewById(R.id.imgViewCardCvvView)).setImageDrawable(getResources().getDrawable(R.drawable.secure2));
                        ((EditText) findViewById(R.id.editCredentialUserEdit)).setInputType(InputType.TYPE_CLASS_TEXT );
                    }
                    else
                    {
                        ((ImageView) findViewById(R.id.imgViewCardCvvView)).setImageDrawable(getResources().getDrawable(R.drawable.not_secure2));
                        ((EditText) findViewById(R.id.editCredentialUserEdit)).setInputType(InputType.TYPE_CLASS_TEXT  | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    }
                }
            });
            ((ImageView) findViewById(R.id.imgViewCardNumberView)).setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view) {
                    if (((EditText) findViewById(R.id.editCredentialPassEdit)).getInputType()!= InputType.TYPE_CLASS_NUMBER)
                    {
                        ((ImageView) findViewById(R.id.imgViewCardNumberView)).setImageDrawable(getResources().getDrawable(R.drawable.secure2));
                        edTxtPass.setInputType(InputType.TYPE_CLASS_TEXT);
                    }
                    else
                    {
                        ((ImageView) findViewById(R.id.imgViewCardNumberView)).setImageDrawable(getResources().getDrawable(R.drawable.not_secure2));
                        edTxtPass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    }
                }
            });
            final Context ctx = this;
            ((Button) findViewById(R.id.btnGenPassCredEdit)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final Dialog dialogGenPass = new Dialog(ctx);
                    dialogGenPass.setContentView(R.layout.gen_pass);
                    dialogGenPass.setTitle(getString(R.string.strPassTitleDialog));
                    //NumberPicker np = ((NumberPicker) dialogGenPass.findViewById(R.id.npLength));  TODO
                    //np.setMinValue(4);
                    //np.setMaxValue(20);
                    ((Button) dialogGenPass.findViewById(R.id.btnGenerate)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            boolean num = ((CheckBox) dialogGenPass.findViewById(R.id.chboxNumber)).isChecked();
                            boolean lChar = ((CheckBox) dialogGenPass.findViewById(R.id.chboxLChar)).isChecked();
                            boolean uChar = ((CheckBox) dialogGenPass.findViewById(R.id.chboxUChar)).isChecked();
                            int length = Integer.valueOf(((EditText) dialogGenPass.findViewById(R.id.genPassLength)).getText().toString());
                            //int length = ((NumberPicker) dialogGenPass.findViewById(R.id.npLength)).getValue();
                            boolean alphaChar = ((CheckBox) dialogGenPass.findViewById(R.id.chBoxAlphaChar)).isChecked();
                            ((TextView) dialogGenPass.findViewById(R.id.txtPassGen)).setText(Functions.genPass(length, num, lChar, uChar, alphaChar));
                        }
                    });

                    ((Button) dialogGenPass.findViewById(R.id.btnOkGenPass)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            edTxtPass.setText(((TextView) dialogGenPass.findViewById(R.id.txtPassGen)).getText().toString());
                            dialogGenPass.dismiss();
                        }
                    });
                    dialogGenPass.show();
                }
            });
        }
    }

    private View.OnClickListener editCredListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String name = ((TextView) findViewById(R.id.txtCredNameViewEdit)).getText().toString();
            String user = ((EditText) findViewById(R.id.editCredentialUserEdit)).getText().toString();
            String pwd = ((EditText) findViewById(R.id.editCredentialPassEdit)).getText().toString();
            String dateCreated=  ((EditText) findViewById(R.id.editCredentialDateCreatedEdit)).getText().toString();
            String dateLastUsed = ((EditText) findViewById(R.id.editCredentialDateLastUsedEdit)).getText().toString();
            String expiry = ((EditText) findViewById(R.id.editCredentialExpiryEdit)).getText().toString();
            String site = ((EditText) findViewById(R.id.editCredentialSiteEdit)).getText().toString();
            String note = ((EditText) findViewById(R.id.editCredentialNoteEdit)).getText().toString();
            boolean test = true;
            if (!Functions.checkUser(user))
            {
                test = false;
                Functions.showShortToast(getBaseContext(), getString(R.string.strCheckUserName));
            }
            if (!Functions.checkPass(pwd))
            {
                test = false;
                Functions.showShortToast(getBaseContext(), getString(R.string.strCheckPassword));
            }
            if (test)
            {
                if (CredentialManager.getInstance().modCredential(name, user, pwd, dateCreated, dateLastUsed,
                                expiry, site, note, sKey))
                {
                    Functions.showLongToast(getBaseContext(), getString(R.string.strOKCreateFile));
                    setResult(Constants.getResultEdit());
                    finish();
                }
                else
                    Functions.showLongToast(getBaseContext(), getString(R.string.strErrorCreateFile));
            }
        }
    };

/*
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.cred_edit, menu);
        return true;
    }*/
    
}
